home *** CD-ROM | disk | FTP | other *** search
/ One Click 27 / CD da revista One Click #27 - Photoshop Megapack (2005).iso / Interface / it.dig / scripts / __Packages / mx / utils / StringFormatter.as < prev    next >
Encoding:
Text File  |  2005-10-28  |  2.5 KB  |  89 lines

  1. class mx.utils.StringFormatter
  2. {
  3.    var __extractToken;
  4.    var __infuseToken;
  5.    var __tokenInfo;
  6.    var __format;
  7.    function StringFormatter(format, tokens, extractTokenFunc, infuseTokenFunc)
  8.    {
  9.       this.setFormat(format,tokens);
  10.       this.__extractToken = extractTokenFunc;
  11.       this.__infuseToken = infuseTokenFunc;
  12.    }
  13.    function extractValue(formattedData, result)
  14.    {
  15.       if(result != null)
  16.       {
  17.          var _loc3_ = null;
  18.          var _loc2_ = 0;
  19.          while(_loc2_ < this.__tokenInfo.length)
  20.          {
  21.             _loc3_ = this.__tokenInfo[_loc2_];
  22.             this.__infuseToken(formattedData.substring(_loc3_.begin,_loc3_.end),_loc3_,result);
  23.             _loc2_ = _loc2_ + 1;
  24.          }
  25.       }
  26.    }
  27.    function formatValue(value)
  28.    {
  29.       var _loc5_ = "";
  30.       if(value != null)
  31.       {
  32.          var _loc3_ = this.__tokenInfo[0];
  33.          _loc5_ = this.__format.substring(0,_loc3_.begin) + this.__extractToken(value,_loc3_);
  34.          var _loc4_ = _loc3_;
  35.          var _loc2_ = 1;
  36.          while(_loc2_ < this.__tokenInfo.length)
  37.          {
  38.             _loc3_ = this.__tokenInfo[_loc2_];
  39.             _loc5_ += this.__format.substring(_loc4_.end,_loc3_.begin) + this.__extractToken(value,_loc3_);
  40.             _loc4_ = _loc3_;
  41.             _loc2_ = _loc2_ + 1;
  42.          }
  43.       }
  44.       return _loc5_;
  45.    }
  46.    function getFormat()
  47.    {
  48.       return this.__format;
  49.    }
  50.    function setFormat(format, tokens)
  51.    {
  52.       function compareValues(a, b)
  53.       {
  54.          if(a.begin < b.begin)
  55.          {
  56.             return -1;
  57.          }
  58.          if(a.begin > b.begin)
  59.          {
  60.             return 1;
  61.          }
  62.          return 0;
  63.       }
  64.       if(format != this.__format)
  65.       {
  66.          this.__format = format;
  67.          var _loc5_ = tokens.split(",");
  68.          this.__tokenInfo = new Array();
  69.          var _loc4_ = 0;
  70.          var _loc3_ = 0;
  71.          var _loc7_ = 0;
  72.          var _loc2_ = 0;
  73.          while(_loc2_ < _loc5_.length)
  74.          {
  75.             _loc4_ = format.indexOf(_loc5_[_loc2_]);
  76.             if(_loc4_ >= 0 && _loc4_ < format.length)
  77.             {
  78.                _loc3_ = format.lastIndexOf(_loc5_[_loc2_]);
  79.                _loc3_ = _loc3_ < 0 ? _loc4_ + 1 : _loc3_ + 1;
  80.                this.__tokenInfo.splice(_loc7_,0,{token:_loc5_[_loc2_],begin:_loc4_,end:_loc3_});
  81.                _loc7_ = _loc7_ + 1;
  82.             }
  83.             _loc2_ = _loc2_ + 1;
  84.          }
  85.          this.__tokenInfo.sort(compareValues);
  86.       }
  87.    }
  88. }
  89.